package com.ymatou.iapi.productitem.testcase;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productitem.service.GetProductInfoByCatalogIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.AssertService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.ProductBasicInfoWapper;
import com.ymttest.database.mongo.ProductStockInfoWapper;
import com.ymttest.database.sqlwapper.YmtCatalogsIWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_GetProductInfoByCatalogId {
	private static String sellerName = EnvSetup.getData("productsellername");
	private static int sellerId = Integer.parseInt(EnvSetup.getData("productsellerid"));
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据规格ID,查询商品信息");
	}

	@Before
	public void caseUp() {
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(new GetProductInfoByCatalogIdCall().getOpurl());
	}
	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductInfoByCatalogId() {
		Logger.start(true, "根据规格ID,查询商品信息");
		try{
			//Map<String,String> tgtAndActKeyPaths = new HashMap<String,String>();
			
			Set<String> keyPathsSet =  new HashSet<String>();
			//1 add new product
			String productId = SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");			
			String catalogsId = new YmtCatalogsIWapper().selectCatalogsByProductId(productId).get(0).getsCatalogId();
			
			JSONObject requestBean = GetProductInfoByCatalogIdCall.GetProductInfoByCatalogId(catalogsId);
			Logger.verifyEquals(200, requestBean.getInt("Code"), "响应报文-返回码");
			 
			
			JSONObject productBean = requestBean.getJSONObject("Data").getJSONObject("Product").getJSONObject("Product");
			Map<String, Object> productBasicInfoMap =  new ProductBasicInfoWapper().getByProductId(productId);
			keyPathsSet.clear();
			keyPathsSet.add("ProductId");
			keyPathsSet.add("SellerName");
			keyPathsSet.add("SellerId");
			keyPathsSet.add("BrandId");
			AssertService.assertResultEqual(keyPathsSet,productBasicInfoMap,productBean);
			
			
			JSONObject catalogStockBean = requestBean.getJSONObject("Data").getJSONObject("Product").getJSONArray("CatalogStockList").getJSONObject(0);
			Map<String, Object> productStockInfoMap =  new ProductStockInfoWapper().getByCatalogId(catalogsId);
			keyPathsSet.clear();
			keyPathsSet.add("ProductId");
			keyPathsSet.add("CatalogId");
			keyPathsSet.add("SKU");
			AssertService.assertResultEqual(keyPathsSet,productStockInfoMap,catalogStockBean);
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
}















